$(function () {
    window.history.forward(1);
    init();
    if(window.location.href.indexOf("test")>=0){
        $("#test").show();
    }
    $("input").on("click",function (e) {
        e.target.focus();
        e.target.select();
    })
})

function init() {
    FastClick.attach(document.body);
    $("#rz").on("focus", "input", function (e) {
        $("#rz input").removeClass("active");
        $(e.target).addClass("active");
    })
    $("#submit").on("click", function () {
        if ($("#telephone").val() == "") {
            $("#telephone").focus();
            return;
        }
        if ($("#validCode").val() == "") {
            $("#validCode").focus();
            return;
        }
        equipmemtService.telephoneAuthentication({
            telephone: $("#telephone").val(),
            validCode: $("#validCode").val()
        }, function (data) {
            if (data.success) {
                if (data.code == "1")
                    window.location.replace("userMain");
                else if (data.code == "0")
                    $.alert(data.errorMsg, function () {
                        window.location.replace("userInfo");
                    })
            } else {
                $.alert(data.errorMsg);
            }
        });
    })
    $("#test").on("click", function () {
        $.modal({
            title: "选择演示身份",
            text: "请选择",
            buttons: [
                {
                    text: "超管", onClick: function () {
                    equipmemtService.telephoneAuthentication({
                        telephone: "13012345675",
                        validCode: "123456"
                    }, function (data) {
                        if (data.success) {
                            if (data.code == "1")
                                window.location.replace(data.errorMsg);
                            else if (data.code == "0")
                                $.alert(data.errorMsg, function () {
                                    window.location.replace("userMain");
                                })
                        } else {
                            $.alert(data.errorMsg);
                        }
                    });
                }
                },
                {
                    text: "维修", className: "bt_warn", onClick: function () {
                    equipmemtService.telephoneAuthentication({
                        telephone: "5001",
                        validCode: "123456"
                    }, function (data) {
                        if (data.success) {
                            if (data.code == "1")
                                window.location.replace(data.errorMsg);
                            else if (data.code == "0")
                                $.alert(data.errorMsg, function () {
                                    window.location.replace("userMain");
                                })
                        } else {
                            $.alert(data.errorMsg);
                        }
                    });
                }
                },
                {
                    text: "库管", className: "bt_warn", onClick: function () {
                    equipmemtService.telephoneAuthentication({
                        telephone: "13012345676",
                        validCode: "123456"
                    }, function (data) {
                        if (data.success) {
                            if (data.code == "1")
                                window.location.replace(data.errorMsg);
                            else if (data.code == "0")
                                $.alert(data.errorMsg, function () {
                                    window.location.replace("userMain");
                                })
                        } else {
                            $.alert(data.errorMsg);
                        }
                    });
                }
                },
                {
                    text: "普通", className: "bt_warn", onClick: function () {
                    equipmemtService.telephoneAuthentication({
                        telephone: "13012345679",
                        validCode: "123456"
                    }, function (data) {
                        if (data.success) {
                            if (data.code == "1")
                                window.location.replace(data.errorMsg);
                            else if (data.code == "0")
                                $.alert(data.errorMsg, function () {
                                    window.location.replace("userMain");
                                })
                        } else {
                            $.alert(data.errorMsg);
                        }
                    });
                }
                },
                {text: "取消", className: "default"},
            ]
        });
    })

    var ua = navigator.userAgent.toLowerCase();
    var isWeixin = ua.indexOf('micromessenger') != -1;
    // isWeixin = false;
    if (!isWeixin) localStorage.clear();
    var model = getQueryString("state");
    if(model=="9"||serverState=="9") localStorage.clear();
    if(getQueryString("userCode")!=null){
        showTelephoneAuthentication();
        //说明已经登录了，但是因为一些原因必须认证后才能使用
        $.alert("此功能需要认证后使用！");
        return;
    }
    var userCode = localStorage.getItem("userCode");
    var loginUrl = window.location.href;
    loginUrl = loginUrl.substring(loginUrl.indexOf("://") + 3);
    loginUrl = loginUrl.substring(0, loginUrl.indexOf("/"));
    var wxUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri=http%3A%2F%2F" + loginUrl + contentPath + "%2Flogin&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
    if (isWeixin && (model == null || model=="9" )&& userCode == null) {
        window.location.replace(wxUrl);
    } else if (model == 1) {
        $("#wxLogin").fadeIn();
        equipmemtService.loginByWxCode({
            code: getQueryString("code")
        }, function (data) {
            if (data.success) {
                //判断认证状态 和目标地址，如果是要去设备页，则直接跳转，如果是去main，如果未认证用户则弹出认证界面，否则直接进入main
                //换句话说，没有认证且要去main的首先进入认证环节，否则不具备去main的能力
                if (data.code == "3" && (data.errorMsg.indexOf("userMain") > -1 )) {
                    showTelephoneAuthentication();
                } else if (data.code == "9") {
                    var msg="此微信用户已被管理员冻结<br/>如需解冻请联系管理员！";
                    $("#subtitle").html("<span style='color: #cc0d11'>"+msg+"</span>");
                    $.alert(msg);
                } else {
                    if(data.code == "3"){
                        $.confirm("是否进行用户认证？","用户认证",function () {
                            showTelephoneAuthentication();
                        },function () {
                            window.location.replace(data.errorMsg);
                        })
                    }else{
                        window.location.replace(data.errorMsg);
                    }
                }
            } else {
                localStorage.clear();
                window.location.replace(wxUrl);
            }
        });
    } else if (userCode != null) {
        $("#wxLogin").fadeIn();
        equipmemtService.loginByWxCode({
            userCode: userCode
        }, function (data) {
            if (data.success) {
                //判断认证状态 和目标地址，如果是要去设备页，则直接跳转，如果是去main，如果未认证用户则弹出认证界面，否则直接进入main
                //换句话说，没有认证且要去main的首先进入认证环节，否则不具备去main的能力
                if (data.code == "3" && (data.errorMsg.indexOf("userMain") > -1 || data.errorMsg.indexOf("favicon.ico") > -1)) {
                    showTelephoneAuthentication();
                } else if (data.code == "9") {
                    var msg="此微信用户已被管理员冻结<br/>如需解冻请联系管理员！";
                    $("#subtitle").html("<span style='color: #cc0d11'>"+msg+"</span>");
                    $.alert(msg);
                } else {
                    window.location.replace(data.errorMsg);
                }
            } else {
                localStorage.clear();
               window.location.replace(wxUrl);
            }
        });
    } else {
        showTelephoneAuthentication();
    }

}

function showTelephoneAuthentication() {
    $("#wx").fadeOut(300, function () {
        $("#rz").fadeIn(300);
        $("#rz input").eq(0).focus();
    });
    $("#subtitle").fadeOut(300, function () {
        $("#subtitle").html("请认证");
        $("#subtitle").fadeIn(300);
    });
}

